<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="日志类型" prop="type">
      <el-input v-model="dataForm.type" placeholder="日志类型"></el-input>
    </el-form-item>
    <el-form-item label="日志标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="日志标题"></el-input>
    </el-form-item>
    <el-form-item label="" prop="serviceId">
      <el-input v-model="dataForm.serviceId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="创建者" prop="createBy">
      <el-input v-model="dataForm.createBy" placeholder="创建者"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="操作IP地址" prop="remoteAddr">
      <el-input v-model="dataForm.remoteAddr" placeholder="操作IP地址"></el-input>
    </el-form-item>
    <el-form-item label="用户代理" prop="userAgent">
      <el-input v-model="dataForm.userAgent" placeholder="用户代理"></el-input>
    </el-form-item>
    <el-form-item label="请求URI" prop="requestUri">
      <el-input v-model="dataForm.requestUri" placeholder="请求URI"></el-input>
    </el-form-item>
    <el-form-item label="操作方式" prop="method">
      <el-input v-model="dataForm.method" placeholder="操作方式"></el-input>
    </el-form-item>
    <el-form-item label="操作提交的数据" prop="params">
      <el-input v-model="dataForm.params" placeholder="操作提交的数据"></el-input>
    </el-form-item>
    <el-form-item label="执行时间" prop="time">
      <el-input v-model="dataForm.time" placeholder="执行时间"></el-input>
    </el-form-item>
    <el-form-item label="删除标记" prop="isDeleted">
      <el-input v-model="dataForm.isDeleted" placeholder="删除标记"></el-input>
    </el-form-item>
    <el-form-item label="异常信息" prop="exception">
      <el-input v-model="dataForm.exception" placeholder="异常信息"></el-input>
    </el-form-item>
    <el-form-item label="" prop="tenantId">
      <el-input v-model="dataForm.tenantId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="所属区域" prop="logArea">
      <el-input v-model="dataForm.logArea" placeholder="所属区域"></el-input>
    </el-form-item>
    <el-form-item label="创建者账号" prop="createNumber">
      <el-input v-model="dataForm.createNumber" placeholder="创建者账号"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          type: '',
          title: '',
          serviceId: '',
          createBy: '',
          createTime: '',
          updateTime: '',
          remoteAddr: '',
          userAgent: '',
          requestUri: '',
          method: '',
          params: '',
          time: '',
          isDeleted: '',
          exception: '',
          tenantId: '',
          logArea: '',
          createNumber: ''
        },
        dataRule: {
          type: [
            { required: true, message: '日志类型不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '日志标题不能为空', trigger: 'blur' }
          ],
          serviceId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建者不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          remoteAddr: [
            { required: true, message: '操作IP地址不能为空', trigger: 'blur' }
          ],
          userAgent: [
            { required: true, message: '用户代理不能为空', trigger: 'blur' }
          ],
          requestUri: [
            { required: true, message: '请求URI不能为空', trigger: 'blur' }
          ],
          method: [
            { required: true, message: '操作方式不能为空', trigger: 'blur' }
          ],
          params: [
            { required: true, message: '操作提交的数据不能为空', trigger: 'blur' }
          ],
          time: [
            { required: true, message: '执行时间不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '删除标记不能为空', trigger: 'blur' }
          ],
          exception: [
            { required: true, message: '异常信息不能为空', trigger: 'blur' }
          ],
          tenantId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          logArea: [
            { required: true, message: '所属区域不能为空', trigger: 'blur' }
          ],
          createNumber: [
            { required: true, message: '创建者账号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`upms/syslog/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 1) {
                this.dataForm.type = data.data.type
                this.dataForm.title = data.data.title
                this.dataForm.serviceId = data.data.serviceId
                this.dataForm.createBy = data.data.createBy
                this.dataForm.createTime = data.data.createTime
                this.dataForm.updateTime = data.data.updateTime
                this.dataForm.remoteAddr = data.data.remoteAddr
                this.dataForm.userAgent = data.data.userAgent
                this.dataForm.requestUri = data.data.requestUri
                this.dataForm.method = data.data.method
                this.dataForm.params = data.data.params
                this.dataForm.time = data.data.time
                this.dataForm.isDeleted = data.data.isDeleted
                this.dataForm.exception = data.data.exception
                this.dataForm.tenantId = data.data.tenantId
                this.dataForm.logArea = data.data.logArea
                this.dataForm.createNumber = data.data.createNumber
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`upms/syslog/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'type': this.dataForm.type,
                'title': this.dataForm.title,
                'serviceId': this.dataForm.serviceId,
                'createBy': this.dataForm.createBy,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'remoteAddr': this.dataForm.remoteAddr,
                'userAgent': this.dataForm.userAgent,
                'requestUri': this.dataForm.requestUri,
                'method': this.dataForm.method,
                'params': this.dataForm.params,
                'time': this.dataForm.time,
                'isDeleted': this.dataForm.isDeleted,
                'exception': this.dataForm.exception,
                'tenantId': this.dataForm.tenantId,
                'logArea': this.dataForm.logArea,
                'createNumber': this.dataForm.createNumber
              })
            }).then(({data}) => {
              if (data && data.code === 1) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
